package itcast.web;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import itcast.domain.PageBean;
import itcast.domain.User;
import itcast.service.UserService;
import itcast.service.UserServiceimpl;

/**
 * Servlet implementation class FindUserByPageServlet
 */
@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindUserByPageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		   request.setCharacterEncoding("utf-8");

	        //1.获取参数
	        String currentPage = request.getParameter("currentPage");//当前页码
	        String rows = request.getParameter("rows");//每页显示条数
	        //、如果从开始页面进入到分页则需要对分页界面进行初始化！
	        if(currentPage == null || "".equals(currentPage)){
	            currentPage = "1";
	        }
	        if(rows == null || "".equals(rows)){
	            rows = "5";
	        }
	        //2.调用service查询
	        UserService service = new UserServiceimpl();
	        PageBean<User> pb = service.findUserByPage(currentPage,rows);
	       
	        System.out.println(pb);
	        //3.将PageBean存入request
	        request.setAttribute("pb",pb);
	        //4.转发到list.jsp
	        request.getRequestDispatcher("userList.jsp").forward(request,response);
	    }
	}

	
